package com.zhiwei.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwei.mapper.UserMapper;
import com.zhiwei.mapper.UserRequestsMapper;
import com.zhiwei.pojo.User;
import com.zhiwei.pojo.UserRequests;
import com.zhiwei.tool.DateUtil;
import com.zhiwei.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.List;

@Service
public class UserRequestsServiceImpl implements UserRequestsService{
    @Autowired
    private UserRequestsMapper userRequestsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult getRequestList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
//        selectPage(分页对象，分页条件)
        QueryWrapper<UserRequests> qw = new QueryWrapper<>();
        qw.like(StringUtils.hasLength(pageResult.getQuery()),"remark", pageResult.getQuery())
                .eq("type", pageResult.getType())
                .eq("delete_flag",false)
                .eq("user_id", pageResult.getUserId());
//        System.out.println(pageResult);
        IPage iPage = userRequestsMapper.selectPage(page, qw);
        List<UserRequests> requests = iPage.getRecords();
        PageResult Result = pageResult.setRows(requests).setTotal(iPage.getTotal());
        return Result;
    }
    @Override
    public PageResult getMyRequests(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
//        selectPage(分页对象，分页条件)
        QueryWrapper<UserRequests> qw = new QueryWrapper<>();
        qw.like(StringUtils.hasLength(pageResult.getQuery()),"remark", pageResult.getQuery())
                .eq("type", pageResult.getType())
                .eq("delete_flag",false)
                .eq("status", 0)
                .eq("leader_id", pageResult.getUserId());
//        System.out.println(pageResult);
        IPage iPage = userRequestsMapper.selectPage(page, qw);
        List<UserRequests> requests = iPage.getRecords();
        PageResult Result = pageResult.setRows(requests).setTotal(iPage.getTotal());
        return Result;
    }

    @Override
    public int updateRequest(UserRequests request) {
        Integer type = request.getType();
        if (type == 2){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("ismanager",1);
            List<User> users = userMapper.selectList(wrapper);
            int i = 0;
            for (User user : users) {
                user.setMsgNum(user.getMsgNum()+1);
                userMapper.updateById(user);
                i++;
            }
        }else {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("ismanager",2);
            List<User> users = userMapper.selectList(wrapper);
            int i = 0;
            for (User user : users) {
                user.setMsgNum(user.getMsgNum()+1);
                userMapper.updateById(user);
                i++;
            }
        }
        request.setStatus(1);
        return userRequestsMapper.updateById(request);
    }
    @Override
    public PageResult getDoneRequests(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
//        selectPage(分页对象，分页条件)
        QueryWrapper<UserRequests> qw = new QueryWrapper<>();
        qw.like(StringUtils.hasLength(pageResult.getQuery()),"remark", pageResult.getQuery())
                .eq("type", pageResult.getType())
                .eq("delete_flag",false)
                .eq("status", 1);
//        System.out.println(pageResult);
        IPage iPage = userRequestsMapper.selectPage(page, qw);
        List<UserRequests> requests = iPage.getRecords();
        PageResult Result = pageResult.setRows(requests).setTotal(iPage.getTotal());
        return Result;
    }

    @Override
    public int agreeRequest(UserRequests request) {
        request.setStatus(2);
        System.out.println(request);
        return userRequestsMapper.updateById(request);
    }

    @Override
    public int addRequest(UserRequests requests) throws ParseException {
        //添加信息
        Integer userId = requests.getUserId();
        User user = userMapper.selectById(userId);
        Integer leaderId = user.getLeaderId();
        User leader = userMapper.selectById(leaderId);
        leader.setMsgNum(leader.getMsgNum()+1);
        userMapper.updateById(leader);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000'Z'");
        SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        if (DateUtil.isDate(requests.getStartType())){
            String format = myDateFormat.format(dateFormat.parse(requests.getStartType()));
            requests.setStartType(format);
        }
        if (DateUtil.isDate(requests.getEndType())){
            String format = myDateFormat.format(dateFormat.parse(requests.getEndType()));
            requests.setEndType(format);
        }
        if (requests.getType()==2){
            requests.setTotal(requests.getTotal()+"元");
        }
        requests.setDeleteFlag(false);
        int insert = userRequestsMapper.insert(requests);
        return insert;
    }

    @Override
    public int deleteRequest(Integer id) {
        UserRequests userRequests = new UserRequests();
        userRequests.setDeleteFlag(true)
        .setId(id);
        return userRequestsMapper.updateById(userRequests);
    }
    //    日期格式检测
}
